<template>
	<view class="container exc-list-container">
		<view class="page-top">
			<view class="page-top-title">我的积分：</view>
			<view class="page-top-main">
				<view class="page-top-num">{{myCoin}}</view>
				<view class="page-top-sign" @click="jump('/pagesUser/exchange/exchange')">
					去签到
					<text class="iconfont">&#xe658;</text>
				</view>
			</view>
			<view class="page-top-log" @click="jump('/pagesUser/exchangeLog/exchangeLog')">
				积分兑换记录
				<text class="iconfont">&#xe775;</text>
			</view>
		</view>
		<view class="list-parent">
			<view class="page-left">
				<view class="page-list" v-for="(item,idx) in pageListLeft" :key="'goods_left_'+idx"
					@click="clickExc(item)">
					<image class="list-img" :src="$fileUrl+'coupon.png'" mode="aspectFill"></image>
					<view class="list-content">
						<view class="list-title">{{item.couponName}}</view>

						<view class="list-count">近期已兑35件</view>
						<view style="position: relative;">
							<text class="list-price">{{item.pointsRequired}}</text>
							<text class="list-tips">积分</text>
							<text class="coupon-type">{{item.couponType}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="page-right">
				<view class="page-list" v-for="(item,idx) in pageListRight" :key="'goods_right_'+idx"
					@click="clickExc(item)">
					<image class="list-img" :src="$fileUrl+'coupon.png'" mode="aspectFill"></image>
					<view class="list-content">
						<view class="list-title">{{item.couponName}}</view>
						<view class="list-count">近期已兑35件</view>
						<view style="position: relative;">
							<text class="list-price">{{item.pointsRequired}}</text>
							<text class="list-tips">积分</text>
							<text class="coupon-type">{{item.couponType}}</text>
						</view>
					</view>

				</view>
			</view>
			<view class="not-more" v-if="isLoading">加载中...</view>
			<view class="null-page" v-else-if="pageList.length<=0 ">
				<text class="iconfont">&#xe642;</text>
				<view class="null-page-tips">暂无数据~</view>
			</view>
			<view class="not-more" v-else-if="notMore">没有更多了...</view>
		</view>
		<view class="list-parent" style="position: fixed;width: 700rpx;left: -800rpx;top: 0;">
			<view class="page-list list-info" :data-idx="idx" v-for="(item,idx) in pageList" :key="'tpl_'+idx">
				<view class="list-title">{{item.couponName}}</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { nextTick, onMounted, ref } from 'vue';
	import { onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app"
	import { hideLoading, showLoading, showToast } from '../../common/util';
	import userApi from '../../common/userApi';
	const isLoading = ref(false)
	const notMore = ref(true)
	const pageSize = 10
	let pageNum = 1
	const pageListLeft = ref([])
	const pageListRight = ref([])
	const pageList = ref([])
	onMounted(async () => {
		getMyCoin()
		await getPageList()
	})
	const myCoin = ref(0)
	const getMyCoin = async () => {
		showLoading()
		try {
			const res = await userApi.getUserPoints()
			myCoin.value = res.data.userPoints
		} finally {
			isLoading.value = false
			hideLoading()
		}
	}
	const loadMore = () => {
		if (isLoading.value) return
		if (notMore.value) return
		pageNum = pageNum + 1
		getPageList()
	}
	const refreshList = () => {
		notMore.value = false
		pageNum = 1
		pageList.value = []
		getPageList()
	}
	onReachBottom(() => {
		loadMore()
	})
	onPullDownRefresh(() => {
		refreshList()
		getMyCoin()
		uni.stopPullDownRefresh()
	})
	const getPageList = async () => {
		showLoading()
		isLoading.value = true
		try {
			const params = { pageNum, pageSize }
			const res = await userApi.getExchangeableCoupons(params)
			const rows = res.data?.rows || {}
			if (rows.length < pageSize) notMore.value = true
			pageList.value = pageList.value.concat(rows)

			// 计算高度 绘制瀑布流
			nextTick(() => {
				const query = uni.createSelectorQuery();
				query.selectAll('.list-info').boundingClientRect((rects : any[]) => {
					let leftHeight = 0
					let rightHeight = 0
					const leftArr = []
					const rightArr = []
					rects.forEach(item => {

						const height = item.height // 模拟高度
						const idx = Number(item.dataset.idx)
						// 找到当前最短的列
						if (leftHeight <= rightHeight) {
							leftHeight += height
							leftArr.push(pageList.value[idx])
						} else {
							rightHeight += height
							rightArr.push(pageList.value[idx])
						}

					});
					pageListRight.value = rightArr
					pageListLeft.value = leftArr
				}).exec();
			})

		} finally {
			isLoading.value = false
			hideLoading()
		}
	}

	const jump = (url : string) => {
		uni.navigateTo({ url: url })
	}
	const clickExc = (item) => {
		if (myCoin.value < item.pointsRequired) {
			showToast('您的积分不足');
			return
		}
		uni.showModal({
			title: '提示',
			content: '你确定要兑换该优惠吗？',
			success: async (res) => {
				if (res.confirm) {
					showLoading()
					const res = await userApi.exchangeCoupon({
						"couponId": item.couponId,
						"quantity": 1
					})
					if(res.code===200){
						showToast('兑换成功');
						getMyCoin()
					}
					hideLoading()
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
</script>

<style lang="scss">
	.exc-list-container {
		padding: 25rpx;
		padding-bottom: 120rpx;
		background-color: #F2F3F8;

		.list-parent {}

		.page-left {
			width: 340rpx;
			display: inline-block
		}

		.page-right {
			margin-left: 20rpx;
			width: 340rpx;
			display: inline-block;
			vertical-align: top;
		}

		.page-list {
			width: 340rpx;
			display: inline-block;
			vertical-align: top;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
		}

		.list-content {
			background-color: #fff;
			padding: 20rpx;
			border-radius: 10rpx;
		}


		.list-img {
			width: 100%;
			height: 340rpx;
			background-color: #F7F7F7;
			//border: solid 1px #eee;
			display: block;
		}

		.list-title {
			font-size: 32rpx;
			color: #202020;
			font-weight: bold;
		}

		.list-count {
			display: inline-block;
			padding: 5rpx 10rpx;
			margin: 15rpx 0;
			background-color: rgba(253, 211, 60, 0.2);
			font-size: 24rpx;
			color: #AC8E23;
		}

		.list-price {
			font-size: 30rpx;
			color: #FF615A;
			font-weight: bold;
		}

		.list-tips {
			font-size: 26rpx;
			color: #FF615A;
			margin-left: 10rpx;
		}

		.page-top-title {
			font-size: 24rpx;
			color: #202020;
			font-weight: bold;
		}

		.page-top-main {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx 0;
		}

		.page-top-num {
			font-size: 84rpx;
			color: #3AD3C1;
			font-weight: bold;
		}

		.page-top-sign {
			height: 60rpx;
			background: rgba(58, 211, 193, 0.2);
			border-radius: 30rpx;
			color: #3AD3C1;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 0 20rpx;
			font-size: 26rpx;
			font-weight: bold;

			.iconfont {
				font-size: 30rpx;
				margin-left: 10rpx;
			}
		}

		.page-top-log {
			padding-bottom: 20rpx;
			font-size: 24rpx;
			color: #6D6D6D;

			.iconfont {
				font-size: 26rpx;
				margin-left: 5rpx;
			}
		}

		.coupon-type {
			position: absolute;
			right: 0;
			bottom: 0;
			font-size: 24rpx;
			background-color: rgba(58, 211, 193, 0.2);
			color: rgba(58, 211, 193, 1);
			padding: 0 5rpx;
			border-radius: 4rpx;
		}
	}
</style>